#include "heltimer.h"
#include "math.h"

HelTimer::HelTimer(QObject *_parent)
{
    connect(this, SIGNAL(timeout()), this, SLOT(inc()));
    this->setInterval(10);
    this->setObjectName("helicoptertimer");
    this->setParent(_parent);
    this->start();
    action = 0; // initialize helicopter to moving right
}

// Move the helicopter side to side
void HelTimer::inc()
{
    switch(action) // Pick which animation action to perform
    {
    case 0: // handle moving right
        {
            if (this->object->pos().x() < ((HelicopterForm*)(this->parent()))->width() - (this->object->width() + 10))
            {
                QPoint pos = this->object->pos();
                this->object->setGeometry(pos.x() + ((HelicopterForm*)(this->parent()))->helSpeed, pos.y(), this->object->width(), this->object->height()); // difficulty level (on form)
            } else
            {
                action = 1;
            }
        }
        break;
    case 1: // handle turning left
        {
            QMovie *tempTest = ((Helicopter*)(this->object))->movie();
            if (tempTest->fileName() == "images/heliright.mng")
            {
                ((Helicopter*)(this->object))->SetHelicopterMovie("images/heliturnleft.mng", true);
            }
            QMovie *tempTestTwo = ((Helicopter*)(this->object))->movie();
            if (tempTestTwo->state() == 0)
            {
                action = 2;
                ((Helicopter*)(this->object))->SetHelicopterMovie("images/helileft.mng", false);
            }
        }
        break;
    case 2: // handle moving left
        {
            if (this->object->pos().x() > 10)
            {
                QPoint pos = this->object->pos();
                this->object->setGeometry(pos.x() - ((HelicopterForm*)(this->parent()))->helSpeed, pos.y(), this->object->width(), this->object->height()); // difficulty level (on form)
            } else
            {
                action = 3;
            }
        }
        break;
    case 3: // handle turning right
        {
            QMovie *tempTest = ((Helicopter*)(this->object))->movie();
            if (tempTest->fileName() == "images/helileft.mng")
            {
                ((Helicopter*)(this->object))->SetHelicopterMovie("images/heliturnright.mng", true);
            }
            QMovie *tempTestTwo = ((Helicopter*)(this->object))->movie();
            if (tempTestTwo->state() == 0)
            {
                action = 0;
                ((Helicopter*)(this->object))->SetHelicopterMovie("images/heliright.mng", false);
            }
        }
        break;
    default:
        break;
    }

    // Handle a screen resize that places the helicopter outside the screen
    if (this->object->pos().x() > ((((HelicopterForm*)(this->parent()))->width() - 5) - this->object->width()))
    {
        ((HelicopterForm*)(this->parent()))->ResetHelicopter();
    }
}

// Setter method for action
void HelTimer::SetAction(int newAction)
{
    action = newAction;
}

// Setter method for action
int HelTimer::GetAction()
{
    return action;
}
